<%@LangUage="VBSCRIPT" CodePage="65001"%>
<%
'赤金CMS www.cjAsp.com All Rights Reserved 
'文件名称：db.inc
'作用：数据库操作、全局第1个引入的文件

Option Explicit '正式上线时请注释这段代码
session.CodePage = 65001
response.charset = "utf-8"
response.Buffer = true

'************以下可按需修改，其它地方请勿修改******************

Const dataPath = "core/database/#cj_data.mdb" '相对于网站根目录 数据库路径，不能以/\.开头，系统会自动在加上路径
Const adminKey = "cjasp" '后台管理认证码

Const cacheDir = "cache" '缓存文件夹名称
Const cacheName = "cjasp" '内存缓存名称(只能为英文)(防止多个网站数据乱而设置不同的名称)
Const viewExt = ".html" '缓存文件的后缀名，必须以.开头 .tpl .asp .htm .html .inc .ini等
dim debug : debug = true '是否记录错误日志

'************以上可按需修改，其它地方请勿修改******************

dim conn, db '数据库对象
dim beginTime, newPic, root, cookieTime, cacheExpireTime
dim adminInfo(10) '管理员信息存放的变量
beginTime = Timer '脚本开始执行的时间
root = "./" '根目录，用于读取缓存文件时，前台为./，如果是后台或多级目录 ../|../../等

set db = new dbModel

class dbModel
	public function open(ByVal path)
		on error resume next
		dim connStr
		if path = "" then path = "./"
		'connStr = "Provider=Sqloledb;user id=sa;password=power;Initial Catalog=new_powerpigs;Data source=NAME\CAT;"
		connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mapPath(path & dataPath)
		set conn = server.createObject("ADODB.connection")
		conn.open connStr
		if err then
			app.errorLog
			err.clear
			close()
			response.write "<font color=red>数据库连接路径出错！</font>"
			response.end()
		end if
	end function

	public function close()
		on error resume next
		if isObject(conn) then
			conn.close
			set conn = nothing
		end if
		set re = nothing
        set fso = nothing
		app.errorLog
	end function

	public function exec(sql, Types)
		on error resume next
		select case Types
		case 1
			set exec = conn.execute(sql)
		case 2
			set exec = server.createObject("adodb.recordset")
			exec.open sql, conn, 1, 1
		case 3
			set exec = server.createObject("adodb.recordset")
			exec.open sql, conn, 1, 3
		case else
			set exec = conn.execute(sql)
		end select
		app.errorLog
	end function

	public function arrayData(sql, ByVal currentPage, ByVal pageSize)
		on error resume next
		dim total, rs, data(1)
		data(0) = 0
		data(1) = null
		if sql <> "" then
			set rs = exec(sql, 2)
			total = rs.recordCount
			if total > 0 then
				rs.absolutePosition = getPagePosition(total, currentPage, pageSize)
				data(0) = total
				data(1) = rs.getRows(pageSize)
			end if
			rs.close
			set rs = nothing
		end if
		app.errorLog
		arrayData = data
	end function

	public function GetData(ByVal sql)
	    on error resume next
		dim rs, data
		data = null
		if not isEmpty(sql) and VarType(sql) = 8 then
			set rs = exec(sql, 1)
			if not rs.eof then data = rs.getRows(-1)
			set rs = nothing
		end if
		app.errorLog
		GetData = data
	end function

    '***************************************************
    'getPagePosition 计算下一页的指针位置
    'total 总数量
    'currentPage 当前页
    'pageSize 每页数
    '返回计算好的指针位置,如果分页值大于项目数，返回1
    '***************************************************
	public function getPagePosition(ByVal total, ByVal currentPage, ByVal pageSize)
		dim totalPage
		totalPage = total \ pageSize
		if total mod rows <> 0 then totalPage = totalPage + 1
		if currentPage > totalPage then currentPage = totalPage
		totalPage = (currentPage - 1) * pageSize + 1
		getPagePosition = totalPage
	end function

end class
%>